package com.jsy.mini12306.ticketsales.service.impl;

import com.jsy.mini12306.common.entity.SeatType;
import com.jsy.mini12306.common.entity.Train;
import com.jsy.mini12306.common.entity.TrainCalendar;
import com.jsy.mini12306.common.entity.TrainPassStation;
import com.jsy.mini12306.ticketsales.bo.DrivingPlan;
import com.jsy.mini12306.ticketsales.service.*;
import com.jsy.mini12306.ticketsales.vo.DrivingPlanVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author: SongyangJi
 * @description:
 * @since: 2021/9/2
 */


@Slf4j
@Service("drivingPlanService")
public class DrivingPlanServiceImpl implements DrivingPlanService {

    @Resource
    TrainService trainService;

    @Resource
    TrainCalendarService calendarService;

    @Resource
    RemainingSeatsService remainingSeatsService;

    @Resource
    TicketService ticketService;


    @Override
    public List<DrivingPlanVO> queryTrainsFromStartCityToEndCity(String startCity, String endCity) {
        return queryTrainsFromStartCityToEndCity(startCity, endCity, LocalDate.now());
    }

    @Override
    public List<DrivingPlanVO> queryTrainsFromStartCityToEndCity(String startCity, String endCity, LocalDate date) {
        // TODO 缓存
        List<Train> trains = trainService.listAllTrains();


//         1. 日期上的过滤
        trains = trains.stream().filter(train -> calendarService.isWorking(train.getTrainId(), date)).collect(Collectors.toList());


        // 如果查询日期在今天后面，无需时间过滤
        LocalTime localTime = date.isAfter(LocalDate.now()) ? LocalTime.parse("00:00:01"): LocalTime.now();
//        LocalTime localTime = LocalTime.parse("05:00:00");


        ArrayList<DrivingPlanVO> plans = new ArrayList<>();
        trains.forEach(train -> {

            DrivingPlan drivingPlan = new DrivingPlan();
            drivingPlan.setTrain(train);
            boolean hasStartCity = false;
            boolean hasEndCity = false;
            for (TrainPassStation trainPassStation : train.getTrainPassStations()) {

                if (Objects.equals(startCity, trainPassStation.getStation().getCity())) {
                    // 2. 时间上的过滤
                    if (trainPassStation.getArrivalTime().isAfter(localTime)) { // 列车此时尚未发车
                        hasStartCity = true;
                        drivingPlan.setDepartStation(trainPassStation);
                    }
                }
                if (hasStartCity && Objects.equals(endCity, trainPassStation.getStation().getCity())) {
                    hasEndCity = true;
                    drivingPlan.setArrivalStation(trainPassStation);
                    break;
                }
            }
            if (hasStartCity && hasEndCity) {
                // 3.余票上的过滤
                Map<SeatType, Integer> seatTypeIntegerMap = remainingSeatsService.checkAvailSeatsOfDrivingPlan(drivingPlan);
                boolean hasTickets = false;
                for (Integer value : seatTypeIntegerMap.values()) {
                    if (value > 0) {
                        hasTickets = true;
                        break;
                    }
                }
                if (hasTickets) {
                    // 设置快照"库存"
                    drivingPlan.setSeatNums(seatTypeIntegerMap);
                    // 查询价格
                    drivingPlan.setSeatPrices(ticketService.queryTicketPriceByDrivingPlan(drivingPlan));
                    plans.add(drivingPlan.getVO());
                }

            }

        });
        return plans;
    }
}
